package com.lucy.property.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.property.entity.HouseShip;
import com.lucy.property.module.park.feerule.ParkFeeRule;
import com.lucy.property.module.park.feerule.ParkFeeRuleService;
import com.lucy.property.module.park.feerule.params.ParkFeeRuleCreateParams;
import com.lucy.property.module.park.feerule.params.ParkFeeRuleQueryParams;
import com.lucy.property.module.park.feerule.params.ParkFeeRuleUpdateParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author kindear
 */
@RestController
@RequestMapping("park/fee")
@Api(tags = "停车收费规则管理")
public class ParkFeeRuleController {
    @Autowired
    ParkFeeRuleService service;

    @PostMapping("rules")
    @ApiOperation(value = "收费规则新建",notes = "")
    public ResponseDTO<Object> ruleCreate(@RequestBody @Validated ParkFeeRuleCreateParams params){
        ParkFeeRule rule = new ParkFeeRule();
        BeanUtil.copyProperties(params,rule, CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return ResponseDTO.succData(
                service.saveOne(rule)
        );
    }

    @DeleteMapping("rules/{id}")
    @ApiOperation(value = "收费规则删除",notes = "")
    public ResponseDTO<Object> ruleDelete(@PathVariable("id") String id){
        service.deleteOne(id);
        return ResponseDTO.succ();
    }

    @PutMapping("rules")
    @ApiOperation(value = "收费规则更新",notes = "")
    public ResponseDTO<Object> ruleUpdate(@RequestBody @Validated ParkFeeRuleUpdateParams params){
        String ruleId = params.getId();
        Optional<ParkFeeRule> ruleOp = service.getOne(ruleId);
        if (!ruleOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        ParkFeeRule rule = ruleOp.get();

        BeanUtil.copyProperties(params,rule,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        return ResponseDTO.succData(
                service.updateOne(rule)
        );
    }

    @GetMapping("rules")
    @ApiOperation(value = "收费规则列表",notes = "")
    public ResponseDTO<Object> ruleListQuery(ParkFeeRuleQueryParams params){
        Sort sort;
        if("DESC".equalsIgnoreCase(params.getOrder())){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        ParkFeeRule item = new ParkFeeRule();
        BeanUtil.copyProperties(params,item,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<ParkFeeRule> example = Example.of(item);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("rules/{id}")
    @ApiOperation(value = "收费规则查询",notes = "")
    public ResponseDTO<Object> ruleQuery(@PathVariable("id") String id){
        Optional<ParkFeeRule> itemOp = service.getOne(id);
        return itemOp.<ResponseDTO<Object>>map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(HttpStatus.NOT_FOUND));
    }

}
